package site.kitcheng.springboottest.test;

import java.security.*;
import java.util.Base64;

/**
 * ClassName: KeyPairGeneratorUtil
 * Package: site.kitcheng.springboottest.test
 * Description:
 *
 * @Author jiangCheng
 * @Create 2025/2/24 11:00
 * @Version 1.0
 */
public class KeyPairGeneratorUtil {
    public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        return keyPairGenerator.generateKeyPair();
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        // 生成密钥对
        KeyPair keyPair = generateKeyPair();
        PrivateKey aPrivate = keyPair.getPrivate();
        // 获取公钥
        PublicKey publicKey = keyPair.getPublic();

        // 将公钥转换为 Base64 字符串
        String publicKeyBase64 = Base64.getEncoder().encodeToString(publicKey.getEncoded());
        String privateKeyBase64 = Base64.getEncoder().encodeToString(aPrivate.getEncoded());


        // 打印 Base64 编码后的公钥
        System.out.println("Public Key (Base64): " + publicKeyBase64);
        System.out.println("Private Key (Base64): " + privateKeyBase64);

    }
}
